package com.sjgl.esdn.controller;

import com.sjgl.esdn.domain.AjaxResultESDN;
import com.sjgl.esdn.domain.entity.Boxroom;
import com.sjgl.esdn.domain.entity.File;
import com.sjgl.esdn.domain.t.FileBoxroom;
import com.sjgl.esdn.service.BoxroomService;
import com.sjgl.esdn.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 收藏夹控制器
 *
 * @author 缪恒铭
 * @date 2022/08/11
 */
@RestController
@RequestMapping("/boxroom")
public class BoxroomController extends BaseControllerESDN {
    @Autowired
    BoxroomService boxroomService;

    @Autowired
    FileService fileService;

    /**
     * 创建收藏夹
     *
     * @param boxroom 收藏夹
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("create")
    public AjaxResultESDN createBoxroom(@RequestBody Boxroom boxroom) {//Long userId,@RequestBody String boxroomName, String intro
        if (boxroomService.insertBoxroom(boxroom.getUserId(), boxroom.getBoxroomName(), boxroom.getIntro()) > 0) {
            return AjaxResultESDN.success("创建成功");
        }
        return AjaxResultESDN.error("收藏夹昵称冲突");
    }

    /**
     * 修改收藏夹
     *
     * @param boxroom 收藏夹
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("rename")
    public AjaxResultESDN renameBoxroom(@RequestBody Boxroom boxroom) {
        if (boxroomService.updateBoxroom(boxroom.getBoxroomId(), boxroom.getBoxroomName(), boxroom.getIntro(), boxroom.getUserId()) > 0) {
            return AjaxResultESDN.success("命名成功");
        }
        return AjaxResultESDN.error("收藏夹昵称冲突");
    }

    /**
     * 删除收藏夹
     *
     * @param boxroomId 收藏夹id
     * @return {@link AjaxResultESDN}
     */
    @DeleteMapping("delete")
    public AjaxResultESDN deleteBoxroom(Long boxroomId) {
        if (boxroomService.deleteBoxroom(boxroomId) > 0) {
            return AjaxResultESDN.success("删除成功");
        }
        return AjaxResultESDN.error("删除失败");
    }

    //参数待检查

    /**
     * 添加文件
     *
     * @param fileBoxroom 文件收藏夹
     * @return {@link AjaxResultESDN}
     */
    @PostMapping("addFile")
    public AjaxResultESDN addFile(@RequestBody FileBoxroom fileBoxroom) {
        Long boxroomId = fileBoxroom.getBoxroomId();
        Long fileId = fileBoxroom.getFileId();
        //检查参数
        if(boxroomService.selectBoxroomById(boxroomId)==null||!fileService.isFileExist(fileId)){
            return AjaxResultESDN.error("收藏夹或文件不存在");
        }

        //检查唯一性
        if (boxroomService.isBoxroomAndFileExist(boxroomId, fileId)) {
            return AjaxResultESDN.error("收藏夹中已存在该文件");
        }
        if (boxroomService.addFile(boxroomId, fileId) > 0) {
            return AjaxResultESDN.success("收藏成功");
        }
        return AjaxResultESDN.error("未知错误");
    }

    //分页

    /**
     * 获得文件列表
     *
     * @param boxroomId 收藏夹id
     * @return {@link AjaxResultESDN}
     * @throws IOException ioexception
     */
    @GetMapping("fileList")
    public AjaxResultESDN getFileList(Long boxroomId) throws IOException {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<Long> fileIdList = boxroomService.selectFileIdListById(boxroomId);
        List<File> fileList = fileService.selectFileListByIdList(fileIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, fileList);
        return ajax;
    }

    /**
     * 删除文件
     *
     * @param boxroomId 收藏夹id
     * @param fileId    文件id
     * @return {@link AjaxResultESDN}
     */
    @DeleteMapping("deleteFile")
    public AjaxResultESDN deleteFile(Long boxroomId, Long fileId) {
        //检查唯一性
        if (!boxroomService.isBoxroomAndFileExist(boxroomId, fileId)) {
            return AjaxResultESDN.error("收藏夹中不存在该文件");
        }
        if (boxroomService.deleteFile(boxroomId, fileId) > 0) {
            return AjaxResultESDN.success("已取消收藏");
        }
        return AjaxResultESDN.error("未知错误");
    }

    /**
     * 获得收藏夹列表
     *
     * @param userId 用户id
     * @return {@link AjaxResultESDN}
     */
    @GetMapping("list")
    public AjaxResultESDN getBoxroomList(Long userId) {
        AjaxResultESDN ajax = AjaxResultESDN.success();
        List<Long> boxroomIdList = boxroomService.selectBoxroomIdListByUserId(userId);
        List<Boxroom> boxroomList = boxroomService.selectBoxroomListByBoxroomIdList(boxroomIdList);
        ajax.put(AjaxResultESDN.DATA_TAG, boxroomList);
        return ajax;
    }
}
